import { _decorator, Component, Animation, PhysicsSystem2D, Rect } from 'cc';
import { Enemy } from '../Enemy';
import { Player } from '../../Player/Player';
const { ccclass, property } = _decorator;

@ccclass('AnimTrigger_Skeleton')
export class AnimTrigger_Skeleton extends Component {
    @property(Enemy) enemy: Enemy = null;
    animation: Animation = null;

    protected start(): void {
        this.animation = this.node.getComponent(Animation);
        this.animation.on(Animation.EventType.FINISHED, this.AnimationTrigger, this);
    }
    public AnimationTrigger(): void {
        this.enemy.stateMgr.currState.AnimationFinishTrigger();
    }
    AttackTrigger() {
        const pos = this.enemy.AttackCheck.getWorldPosition();
        //偏移问题
        const box = new Rect(pos.x + (this.enemy.facingDir - 1) * this.enemy.AttackCheckR / 2, pos.y, this.enemy.AttackCheckR, this.enemy.AttackCheckR);
        const colliderList = PhysicsSystem2D.instance.testAABB(box);
        for (const collider of colliderList) {
            const playerComponent = collider.getComponent(Player);
            if (playerComponent) {
                console.info(collider.name);
                playerComponent.damage();
                return;
            }
        }
    }
    CloseCounterWindow() {
        this.enemy.CloseCounterAttackWindow()
    }
    OpenCounterWindow() {
        this.enemy.OpenCounterAttackWindow();
    }
}


